/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_club.js                            
*  Description  : 마이홈 동호회정보
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-12  김효순    Created.  
*  2012-05-12  김효순    Complete.
*******************************************************************************/

jQuery(function($){
	// 동호회 새글
	function clsNewList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsNewList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home:eq(0) tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("tr").each(function(n){
				$(this).find(">td:eq(0)").html("<a href='#'>" + thisClass.listData[n]["동호회명"] + "</a>");
				$(this).find(">td:eq(1)").html(thisClass.listData[n]["새글수"]);
				$(this).find(">td:eq(2)").html(thisClass.listData[n]["시간"]);
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent("table").next("div#tmpSpin").length == 0){
				$layer.parent("table").after("<div id='tmpSpin' style='position:relative; width:1000px; height:100px;' />");
			}
			$layer.parent("table").next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent("table").next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
		, $getBtnTitles: function(){
			return this._HTMLDom["$layer"].find("tr>td:first-child a");
		}
	}
	
	// 최근 가입 동호회
	function clsNewClubList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsNewClubList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home:eq(1) div.my_k_alim:eq(0) tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("tr").each(function(n){
				$(this).find(">td:eq(0)").html("<a href='#'>" + thisClass.listData[n]["동호회명"] + "</a>");
				$(this).find(">td:eq(1)").html("가입일 : " + thisClass.listData[n]["가입일"]);
				$(this).find(">td:eq(2)").html(thisClass.listData[n]["내상태"]);
				
				switch(thisClass.listData[n]["내상태"]){
					case "일반회원": 
						$(this).find(">td:eq(3)").html("<a href='#'>탈퇴하기</a>");
						break;
					case "시솝": 
						$(this).find(">td:eq(3)").html("<a href='#'>양도하기</a>");
						break;
				}
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent("table").next("div#tmpSpin").length == 0){
				$layer.parent("table").after("<div id='tmpSpin' style='position:relative; width:1000px; height:100px;' />");
			}
			$layer.parent("table").next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent("table").next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
		, $getBtnTitles: function(){
			return this._HTMLDom["$layer"].find("tr>td:first-child a");
		}
		, $getBtnActions: function(){
			return this._HTMLDom["$layer"].find("tr>td:nth-child(4) a");
		}
		, getClubKey: function(idx){
			return this.listData[idx]["동호회키"];
		}
	}
	
	// 동호회
	function clsClubList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsClubList.prototype = {
		_init: function(){
			var $layer = $("div#container section#my_home:eq(1) div.my_k_alim:eq(1) tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("tr").each(function(n){
				$(this).find(">td:eq(0)").html("<a href='#'>" + thisClass.listData[n]["동호회명"] + "</a>");
				$(this).find(">td:eq(1)").html("가입일 : " + thisClass.listData[n]["가입일"]);
				$(this).find(">td:eq(2)").html(thisClass.listData[n]["내상태"]);
				
				switch(thisClass.listData[n]["내상태"]){
					case "일반회원": 
						$(this).find(">td:eq(3)").html("<a href='#'>탈퇴하기</a>");
						break;
					case "시솝": 
						$(this).find(">td:eq(3)").html("<a href='#'>양도하기</a>");
						break;
				}
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent("table").next("div#tmpSpin").length == 0){
				$layer.parent("table").after("<div id='tmpSpin' style='position:relative; width:1000px; height:100px;' />");
			}
			$layer.parent("table").next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent("table").next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
		, $getBtnTitles: function(){
			return this._HTMLDom["$layer"].find("tr>td:first-child a");
		}
		, $getBtnActions: function(){
			return this._HTMLDom["$layer"].find("tr>td:nth-child(4) a");
		}
		, getClubKey: function(idx){
			return this.listData[idx]["동호회키"];
		}
	}
	
	// 서버통신
	function clsComm()
	{
		// 동호회 새글
		this.newList = null;
		this.newPageData = null;
		this.newRequest = null;
		// 최근 가입 동호회
		this.newClubList = null;
		// 동호회 리스트
		this.clubList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.newList = [
				{
					"동호회키": null
					, "동호회명": null
					, "새글수": null
					, "시간": null
				}
			];
			
			this.newRequest = {
				"페이지": null
				, "갯수": null
			}
			
			this.newClubList = [
				{
					"동호회키": null
					, "동호회명": null
					, "가입일": null
					, "내상태": null
				}
			];
			
			this.clubList = [
				{
					"동호회키": null
					, "동호회명": null
					, "가입일": null
					, "내상태": null
				}
			];
		}
		, setNewRequest: function(key, val){
			if((typeof arguments[0]).toUpperCase() == "object"){
				this.newRequest = arguments[0];
			}else{
				this.newRequest[key] = val;
			}
		}
		, getNewList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.newPageData = {
					"전체수": 11
				}
				
				thisClass.newList = [
					{
						"동호회키": "001"
						, "동호회명": "♥가장 편안한 [골프 커뮤니티] 왕골스토리♥ 대표1"
						, "새글수": "1"
						, "시간": "30분 전"
					}
					, {
						"동호회키": "002"
						, "동호회명": "♥가장 편안한 [골프 커뮤니티] 왕골스토리♥ 대표2"
						, "새글수": "2"
						, "시간": "1시간 전"
					}
					, {
						"동호회키": "003"
						, "동호회명": "♥가장 편안한 [골프 커뮤니티] 왕골스토리♥ 대표3"
						, "새글수": "3"
						, "시간": "3일 전"
					}
					, {
						"동호회키": "004"
						, "동호회명": "♥가장 편안한 [골프 커뮤니티] 왕골스토리♥ 대표4"
						, "새글수": "4"
						, "시간": "34분 전"
					}
				];
				
				document.objControl.finishNewList();
			}, 1500);
		}
		, getNewData: function(){
			return this.newList;
		}
		, getNewPageData: function(){
			return this.newPageData;
		}
		, getNewClubList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.newClubList = [
					{
						"동호회키": "001"
						, "동호회명": "타이틀리스트에 미치다! (Mad for Titleist!)1"
						, "가입일": "2012.04.10"
						, "내상태": "일반회원"
					}
					, {
						"동호회키": "002"
						, "동호회명": "타이틀리스트에 미치다! (Mad for Titleist!)2"
						, "가입일": "2012.04.11"
						, "내상태": "시솝"
					}
					, {
						"동호회키": "003"
						, "동호회명": "타이틀리스트에 미치다! (Mad for Titleist!)3"
						, "가입일": "2012.04.13"
						, "내상태": "일반회원"
					}
				];
				
				document.objControl.finishNewClubList();
			}, 1500);
		}
		, getNewClubData: function(){
			return this.newClubList;
		}
		, getClubList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.clubList = [
					{
						"동호회키": "001"
						, "동호회명": "골프할인 골프몬1"
						, "가입일": "2012.04.10"
						, "내상태": "일반회원"
					}
					, {
						"동호회키": "002"
						, "동호회명": "골프할인 골프몬2"
						, "가입일": "2012.04.11"
						, "내상태": "시솝"
					}
					, {
						"동호회키": "003"
						, "동호회명": "골프할인 골프몬3"
						, "가입일": "2012.04.13"
						, "내상태": "일반회원"
					}
				];
				
				document.objControl.finishClubList();
			}, 1500);
		}
		, getClubData: function(){
			return this.clubList;
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objNewList = null;
		this.objNewClubList = null;
		this.objClubList = null;
		this.objNewListPage = null;

		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objNewList = new clsNewList();
			this.objNewClubList = new clsNewClubList();
			this.objClubList = new clsClubList();
			
			$("ul.pagenum_area").wrap("<div id='idNewListPage' />").remove();
			this.objNewListPage = new document.objGlobal.clsPaging($("div#idNewListPage"), 10, 10);
			
			this._bind();
			
			// 동호회 새글
			this.objNewList.spinStart();
			this.objComm.getNewList();
			this.objNewListPage.setPage(1);
			
			// 최근가입한 동호회
			this.objNewClubList.spinStart();
			this.objComm.getNewClubList();
			
			// 동호회 리스트
			this.objClubList.spinStart();
			this.objComm.getClubList();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 동호회 새글 클릭
			this.objNewList.$getBtnTitles().live("click", function(){
				var n = thisClass.objNewList.$getBtnTitles().index($(this));
				
				alert(n);
			});
			
			// 최근 가입 동호회 클릭
			this.objNewClubList.$getBtnTitles().live("click", function(){
				var n = thisClass.objNewClubList.$getBtnTitles().index($(this));
				
				alert(n);
			});
			
			// 최근 가입 동호회 탈퇴/양도 클릭
			this.objNewClubList.$getBtnActions().live("click", function(){
				var n = thisClass.objNewClubList.$getBtnActions().index($(this));
				
				switch($(this).text()){
					case "탈퇴하기": 
						thisClass.openClubWithdraw(thisClass.objNewClubList.getClubKey(n));
						break;
					case "양도하기": 
						thisClass.clubTransfer(thisClass.objNewClubList.getClubKey(n));
						break;
				}
			});
			
			// 동호회 클릭
			this.objClubList.$getBtnTitles().live("click", function(){
				var n = thisClass.objClubList.$getBtnTitles().index($(this));
				
				alert(n);
			});
			
			// 동호회 탈퇴/양도 클릭
			this.objClubList.$getBtnActions().live("click", function(){
				var n = thisClass.objClubList.$getBtnActions().index($(this));
				
				switch($(this).text()){
					case "탈퇴하기": 
						thisClass.openClubWithdraw(thisClass.objClubList.getClubKey(n));
						break;
					case "양도하기": 
						thisClass.clubTransfer(thisClass.objClubList.getClubKey(n));
						break;
				}
			});
			
			// 페이지이동
			$(this.objNewListPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setNewRequest({
					"페이지": thisPage
					, "갯수": this.getCountPerPage()
				});
				thisClass.objNewList.spinStart();
				thisClass.objComm.getNewList();
			});
		}
		// 동호회 새글 완료
		, finishNewList: function(){
			var thisData = this.objComm.getNewData();
			var pageData = this.objComm.getNewPageData();
			
			this.objNewList.spinEnd();
			this.objNewList.setList(thisData);
			this.objNewListPage.setTotalCount(pageData["전체수"]);
			this.objNewListPage.display();
		}
		// 최근가입한 동호회 완료
		, finishNewClubList: function(){
			var thisData = this.objComm.getNewClubData();

			this.objNewClubList.spinEnd();
			this.objNewClubList.setList(thisData);
		}
		// 동호회리스트 완료
		, finishClubList: function(){
			var thisData = this.objComm.getClubData();
				
			this.objClubList.spinEnd();
			this.objClubList.setList(thisData);
		}
		// 동호회 탈퇴
		, openClubWithdraw: function(clubKey){
			var winClubPop = window.open("../popup/pop_club_sece.html?q_key=" + clubKey, "winClubPop", "width=400, height=100, scrollbars=0");
			if(winClubPop) winClubPop.focus();
		}
		// 동호회 탈퇴 완료
		, clubSeceRet: function(ret){
			document.location.reload();
		}
		// 동호회 양도
		, clubTransfer: function(clubKey){
			alert(clubKey + "양도");
		}
	}
	
	document.objControl = new clsControl();
});
